<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="login-box">
        <div class="title">登录页</div>
        <div class="block">
            <input type="text" placeholder="输入用户名">
        </div>
        <div class="block">
            <input type="password" placeholder="输入密码">
        </div>
        <div class="block">
            <input type="button" value="登录">
        </div>
    </div>
</body>

</html>
<script src="../promise-ajax.js"></script>
<script>
    //获取input值
    var inputs = document.querySelectorAll('.login-box .block input')
    console.log(inputs);

    // 给button添加点击事件
    inputs[2].onclick = function () {

        // 表单验证
        if (inputs[0].value && inputs[1].value) {
            if (/^\d{4}$/.test(inputs[1].value)) {
                ajax('get', 'https://yantianfeng.com/api/login', { username: inputs[0].value, password: inputs[1].value }).then((data) => {
                    if (data.status == 200) {
                        alert('登陆成功')
                    } else {
                        alert('登陆失败')
                    }
                })
            } else {
                alert('请输入正确的手机号/密码')
            }
        } else {
            alert('用户名/密码不能为空')
        }
    }
</script>